package com.neusoft.nep.common;
import com.neusoft.nep.admin.entity.Administrator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {
    public FileUtils() {
    }

    public static <K, V> Map<K, V> readMapObject(String fileName) {
        File file = new File(fileName);
        Map map = null;

        try {
            if (file.length() == 0L) {
                map = new HashMap();
            } else {
                FileInputStream fis = null;
                fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                map = (Map)ois.readObject();
            }

            return map;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeMapObject(String fileName, Map map) {
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(map);
            oos.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
